import { axiosInstance } from 'src/boot/axios'
import CommonConfig from '../../assets/js/config'

const upmsUrl = CommonConfig.upmsUrl + '/v1'
const planUrl = CommonConfig.planningUrl + '/v1'
const datacenterUrl = CommonConfig.datacenterUrl + '/v1'

import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

/**
 * 获取部门 tree
 * @return {Promise<unknown>} promise
 */
export function getDepartmentsTree () {
  const url = upmsUrl + '/department_tree'

  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 获取部门下人员列表
 * @param departmentId 部门ID
 */
export function getPeople (departmentId) {
  const url = datacenterUrl + `/departments/${departmentId}/users`
  const reqData = {
    params: {
      includeSubordinate: true // 显示下级部门人员
    }
  }

  return axiosInstance.get(url, reqData).then(resp => {
    return Promise.resolve(resp.data)
  })
}

// 获取人员绩效列表
export function getPerformanceReports (query) {
  // console.log(query, 'query')
  const reqData = {
    params: {
      since: query.since,
      before: query.before,
      operatorIds: query.operatorIds || [],
      pageNum: query.pageNum,
      pageSize: query.pageSize,
      departmentId: query.departmentId || null
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const newUrl = planUrl + '/employee_performance_reports'
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

// 人员绩效导出
export function downloadPerformance (params) {
  // console.log(params, 111)
  // console.log(JSON.stringify(params), 222)
  const newUrl = planUrl + '/employee_performance_reports/excel:export'
  let reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.post(newUrl, reqData.params).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取人员绩效详情列表
export function getPerformanceDetail (query) {
  // console.log(query, 111)
  const reqData = {
    params: {
      since: query.since,
      before: query.before,
      operatorIds: query.operatorId,
      pageNum: query.pageNum,
      pageSize: query.pageSize
    }
  }
  const newUrl = planUrl + '/employee_performance_report_items'
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

// 人员绩效详情导出
export function downloadPerformanceDetail (params) {
  // console.log(JSON.stringify(params), 222)
  const newUrl = planUrl + '/employee_performance_report_items/excel:export'
  let reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  console.log(reqData.params)
  return axiosInstance.post(newUrl, reqData.params).then((res) => {
    return Promise.resolve(res)
  })
}
